package com.lingeringdream.luciole.model.helper;

import com.lingeringdream.luciole.model.entity.*;
import ohos.data.rdb.ValuesBucket;

/**
 * ValuesBucket和实体类转换
 *
 * @author Kirky
 * @version 1.0
 * @date 2022/4/28
 */
public class ValuesBucketHelper {

    /**
     * User实体转ValuesBucket
     *
     * @param user user实体
     * @return ValuesBucket
     */
    public static ValuesBucket user2Bucket(User user) {
        ValuesBucket bucket = new ValuesBucket();
        bucket.putLong("uid", user.getUid());
        bucket.putString("unionid", user.getHuaweiId());
        bucket.putString("tel", user.getTel());
        bucket.putString("uname", user.getUname());
        bucket.putString("password", user.getPassword());
        bucket.putBoolean("gender", user.getGender());
        bucket.putLong("registration", user.getRegistration());
        bucket.putLong("birth", user.getBirth());
        bucket.putString("brief", user.getBrief());
        bucket.putString("pkey", user.getPkey());
        bucket.putString("avatars", user.getAvatars());
        bucket.putLong("update", user.getUpdate());
        return bucket;
    }

    /**
     * ValuesBucket转User实体
     *
     * @param bucket 数据库实体
     * @return User
     */
    public static User bucket2User(ValuesBucket bucket) {
        User user = new User();
        user.setUid(bucket.getLong("uid"));
        user.setHuaweiId(bucket.getString("union_id"));
        user.setTel(bucket.getString("tel"));
        user.setUname(bucket.getString("uname"));
        user.setGender(bucket.getBoolean("gender"));
        user.setPassword(bucket.getString("password"));
        user.setRegistration(bucket.getLong("registration"));
        user.setBirth(bucket.getLong("birth"));
        user.setBrief(bucket.getString("brief"));
        user.setPkey(bucket.getString("pkey"));
        user.setAvatars(bucket.getString("avatars"));
        user.setUpdate(bucket.getLong("update"));
        return user;
    }

    /**
     * Classification实体转ValuesBucket
     *
     * @param clazz 实体
     * @return ValuesBucket
     */
    public static ValuesBucket class2Bucket(Classification clazz) {
        ValuesBucket bucket = new ValuesBucket();
        bucket.putLong("cid", clazz.getCid());
        bucket.putString("ename", clazz.geteName());
        bucket.putString("cname", clazz.getcName());
        bucket.putInteger("class", clazz.getClazz());
        bucket.putString("harmful", clazz.getHarmful());
        bucket.putString("suggestion", clazz.getSuggestion());
        bucket.putLong("update", clazz.getUpdate());
        return bucket;
    }

    /**
     * ValuesBucket转Classification实体
     *
     * @param bucket 数据库实体
     * @return Classification
     */
    public static Classification bucket2Class(ValuesBucket bucket) {
        Classification c = new Classification();
        c.setCid(bucket.getLong("cid"));
        c.seteName(bucket.getString("ename"));
        c.setcName(bucket.getString("cname"));
        c.setClazz(bucket.getInteger("class"));
        c.setHarmful(bucket.getString("harmful"));
        c.setSuggestion(bucket.getString("suggestion"));
        c.setUpdate(bucket.getLong("update"));
        return c;
    }

    /**
     * Classification实体转ValuesBucket
     *
     * @param news 实体
     * @return ValuesBucket
     */
    public static ValuesBucket news2Bucket(News news) {
        ValuesBucket bucket = new ValuesBucket();
        bucket.putLong("nid", news.getNid());
        bucket.putString("title", news.getTitle());
        bucket.putString("label", news.getLabel());
        bucket.putLong("belong", news.getBelong());
        bucket.putLong("publication", news.getPublicationTime());
        bucket.putString("picuri", news.getPicUri());
        bucket.putInteger("cardmode", news.getCardMode());
        return bucket;
    }

    /**
     * ValuesBucket转Classification实体
     *
     * @param bucket 数据库实体
     * @return News
     */
    public static News bucket2News(ValuesBucket bucket) {
        News news = new News();
        news.setNid(bucket.getLong("nid"));
        news.setTitle(bucket.getString("title"));
        news.setBelong(bucket.getLong("belong"));
        news.setLabel(bucket.getString("label"));
        news.setPublicationTime(bucket.getLong("publication"));
        news.setPicUri(bucket.getString("picuri"));
        news.setCardMode(bucket.getByte("cardmode"));
        return news;
    }

    /**
     * Level实体转ValuesBucket
     *
     * @param level 实体
     * @return ValuesBucket
     */
    public static ValuesBucket level2Bucket(Level level) {
        ValuesBucket bucket = new ValuesBucket();
        bucket.putInteger("level", level.getLevel());
        bucket.putString("name", level.getName());
        bucket.putInteger("score", level.getScore());
        bucket.putInteger("experience", level.getExperience());
        bucket.putString("description", level.getDescription());
        bucket.putLong("get_time", level.getGetTime());
        return bucket;
    }

    /**
     * ValuesBucket转Level实体
     *
     * @param bucket 数据库实体
     * @return Level
     */
    public static Level bucket2Level(ValuesBucket bucket) {
        Level level = new Level();
        level.setLevel(bucket.getInteger("level"));
        level.setName(bucket.getString("name"));
        level.setScore(bucket.getInteger("score"));
        level.setExperience(bucket.getInteger("experience"));
        level.setDescription(bucket.getString("description"));
        level.setGetTime(bucket.getLong("get_time"));
        return level;
    }

    /**
     * Achievement实体转ValuesBucket
     *
     * @param achievement 实体
     * @return ValuesBucket
     */
    public static ValuesBucket achievement2Bucket(Achievement achievement) {
        ValuesBucket bucket = new ValuesBucket();
        bucket.putLong("achievement_id", achievement.getAchievementId());
        bucket.putString("name", achievement.getName());
        bucket.putString("description", achievement.getDescription());
        bucket.putString("condition", achievement.getCondition());
        bucket.putString("icon", achievement.getIcon());
        bucket.putInteger("type", achievement.getType());
        bucket.putLong("get_time", achievement.getGetTime());
        return bucket;
    }

    /**
     * ValuesBucket转Achievement实体
     *
     * @param bucket 数据库实体
     * @return Achievement
     */
    public static Achievement bucket2Achievement(ValuesBucket bucket) {
        Achievement achievement = new Achievement();
        achievement.setAchievementId(bucket.getLong("achievement_id"));
        achievement.setName(bucket.getString("name"));
        achievement.setDescription(bucket.getString("description"));
        achievement.setCondition(bucket.getString("condition"));
        achievement.setIcon(bucket.getString("icon"));
        achievement.setType(bucket.getInteger("type"));
        achievement.setGetTime(bucket.getLong("get_time"));
        return achievement;
    }

    /**
     * Privates实体转ValuesBucket
     *
     * @param privates 实体
     * @return ValuesBucket
     */
    public static ValuesBucket private2Bucket(Privates privates) {
        ValuesBucket bucket = new ValuesBucket();
        bucket.putString("name", privates.getName());
        bucket.putString("purpose", privates.getPurpose());
        bucket.putInteger("type", privates.getType());
        bucket.putLong("last", privates.getLast());
        bucket.putInteger("count", privates.getCount());
        return bucket;
    }

    /**
     * ValuesBucket转Privates实体
     *
     * @param bucket 数据库实体
     * @return Privates
     */
    public static Privates bucket2Private(ValuesBucket bucket) {
        Privates privates = new Privates();
        privates.setName(bucket.getString("name"));
        privates.setPurpose(bucket.getString("purpose"));
        privates.setType(bucket.getInteger("type"));
        privates.setLast(bucket.getLong("last"));
        privates.setCount(bucket.getInteger("count"));
        return privates;
    }

    /**
     * Message转ValuesBucket
     *
     * @param message 实体
     * @return ValuesBucket
     */
    public static ValuesBucket message2Bucket(Message message) {
        ValuesBucket bucket = new ValuesBucket();
        bucket.putLong("mid", message.getMid());
        bucket.putString("title", message.getTitle());
        bucket.putString("content", message.getContent());
        bucket.putInteger("type", message.getType());
        bucket.putBoolean("status", message.getStatus());
        bucket.putLong("time", message.getTime());
        return bucket;
    }

    /**
     * ValuesBucket转Message实体
     *
     * @param bucket 数据库实体
     * @return Message
     */
    public static Message bucket2Message(ValuesBucket bucket) {
        Message message = new Message();
        message.setMid(bucket.getLong("mid"));
        message.setTitle(bucket.getString("title"));
        message.setContent(bucket.getString("content"));
        message.setType(bucket.getInteger("type"));
        message.setStatus(bucket.getBoolean("status"));
        message.setTime(bucket.getLong("time"));
        return message;
    }
}
